package com.wing.api.query.orderReceiveLoad.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.query.orderReceiveLoad.BaseQueryOrderReceiveLoadService;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.order.model.entity.OrderReceiveLoad;
import com.wing.order.model.entity.OrderReceiveLoadDetail;
import com.wing.order.model.request.OrderReceiveLoadForm;
import com.wing.order.model.response.OrderReceiveLoadDetailVO;
import com.wing.order.service.OrderReceiveLoadDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 标准上新查询接口
 */
@Service
@Slf4j
public class StandardQueryOrderReceiveLoadServiceImpl implements BaseQueryOrderReceiveLoadService {

    @Override
    public PageResult<OrderReceiveLoadDetailVO> queryList(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo, OrderReceiveLoadDetailService orderReceiveLoadDetailService) {
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        Integer loadType = jsonObject.getInteger("loadType");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String cabCode = jsonObject.getString("cabCode");
        String spaceCode = jsonObject.getString("spaceCode");
        String skuCode = jsonObject.getString("skuCode");

        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }

        if(page == null || page == 0){
            throw new BusinessException("page参数传值异常！");
        }
        if(limit == null || limit == 0){
            throw new BusinessException("limit参数传值异常！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);
        if(null != loadType){
            tempStr.append(loadType);
        }
        if(StringUtils.isNotBlank(startTime)){
            tempStr.append(startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            tempStr.append(endTime);
        }
        if(StringUtils.isNotBlank(cabCode)){
            tempStr.append(cabCode);
        }
        if(StringUtils.isNotBlank(spaceCode)){
            tempStr.append(spaceCode);
        }
        if(StringUtils.isNotBlank(skuCode)){
            tempStr.append(skuCode);
        }
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }

        Page<OrderReceiveLoadDetail> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("sort", "create_time");
        params.put("order", "desc");
        if(StringUtils.isNotBlank(startTime)){
            params.put("startCreateTime", startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            params.put("endCreateTime", endTime);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabCode",cabCode);
        }
        if(StringUtils.isNotBlank(spaceCode)){
            params.put("spaceCode",spaceCode);
        }
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode",skuCode);
        }
        if (loadType!=null){
            if (OrderReceiveLoadForm.LoadType.load.getValue().equals(loadType)){
                params.put("types",new Integer[]{OrderReceiveLoad.Type.selfLoad.getValue(),OrderReceiveLoad.Type.selfLoadSimple.getValue(),OrderReceiveLoad.Type.selfBatchLoadSimple.getValue()});
            }else{
                params.put("types",new Integer[]{OrderReceiveLoad.Type.selfUnloadSimple.getValue(),OrderReceiveLoad.Type.selfBatchUnloadSimple.getValue()});
            }
        }
        IPage<OrderReceiveLoadDetailVO> list = orderReceiveLoadDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @Override
    public JsonResult queryDetail(Long id, EnterpricceIdVO enterpricceIdVo, String sign) {
        log.info("请求参数,id="+id+",sign="+sign);
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）

        if(StringUtils.isBlank(sign)){
            throw new BusinessException("签名值为空！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(id);
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", tempStr.toString());
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sign)){
            throw new BusinessException("签名不通过！");
        }
        EnterpriseClient enterpriseClient = (EnterpriseClient) ApplicationContextUtil.getBean("enterpriseClientFallback");

        return enterpriseClient.foreignProductInfoDetail(id);
    }

    public static void main(String[] args) {
        String key = "e10adc3949ba59abbe56e057f20f883e"; // 本地
//        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb."; // 测试
//        String key = "$2a$10$A4q7sKe4/3GekaIIHLTqG.Gd4y8PhX4yLNleuHK9fvvyWq1vodM6e";
        Integer page = 1;
        Integer limit = 10;

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);
        tempStr.append(0);

        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
//        log.info("签名目的串,{}", desSign);
        System.out.println(desSign);
    }

}
